package dj.MyNote;



import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.format.Time;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MyNoteEdit extends Activity {
	Button btnSave = null;
	Button btnClean = null;
	EditText edtTitle = null;
	EditText edtContent = null;
	
	DataBaseHelper dbHelper = null;
	SQLiteDatabase db = null;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.edit);
        System.out.println("dj--- MyNoteEdit.onCreate");
        btnSave = (Button)findViewById(R.id.ButtonSave);
        btnClean = (Button)findViewById(R.id.ButtonClean);
        edtTitle = (EditText)findViewById(R.id.EditTextTitle);
        edtContent = (EditText)findViewById(R.id.EditTextContent);
        
        btnSave.setOnClickListener(new SaveButtonListener());
        btnClean.setOnClickListener(new CleanButtonListener());
        
        dbHelper = new DataBaseHelper(MyNoteEdit.this,"dj_note_db");
		db = dbHelper.getWritableDatabase();
    }
    
    
    @Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
    	db.close();
		super.onDestroy();
	}
    
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if(keyCode == KeyEvent.KEYCODE_BACK){
			Intent intent = new Intent();
			intent.setClass(MyNoteEdit.this, MyNoteListShow.class);
			MyNoteEdit.this.startActivity(intent);
			MyNoteEdit.this.finish();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}




	class SaveButtonListener implements OnClickListener{

		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			
			Cursor cursor = db.query("MyNoteData", new String[]{"max(id)+1"}, null, null, null, null, null);
			cursor.moveToFirst();
			Time time = new Time();
			time.setToNow();
			
			
			ContentValues values = new ContentValues(); 
			values.put("id", cursor.getInt(0));
			values.put("title", edtTitle.getText().toString());
			values.put("content", edtContent.getText().toString());
			values.put("createtime",time.toString());
			values.put("modifytime",time.toString());
			
			db.insert("MyNoteData", null, values);
			
			Intent intent = new Intent();
			intent.setClass(MyNoteEdit.this, MyNoteListShow.class);
			MyNoteEdit.this.startActivity(intent);
			MyNoteEdit.this.finish();
			
		}
    }
    class CleanButtonListener implements OnClickListener{

		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			edtTitle.setText(null);
			edtContent.setText(null);
			
		}
    }

    
}